﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CInChat.Communication;

namespace CInChat.UI
{
    public partial class MainWindow : Window
    {
        private ChatController chatController;

        public MainWindow() {
            InitializeComponent();
            this.Loaded += mainWindow_Loaded;
            this.Closing += mainWindow_Closing;
        }

        private void mainWindow_Loaded(object sender, RoutedEventArgs e) {
            this.Title += " - " + App.User;
            this.chatController = new ChatController(App.User);
            this.chatController.Started += chatController_Started;
            this.chatController.UserJoined += chatController_UserJoined;
            this.chatController.UserLeft += chatController_UserLeft;
            this.chatController.MessageReceived += chatController_MessageReceived;
            this.chatController.ListUpdated += chatController_ListUpdated;
            this.chatController.Whipered += chatController_Whipered;
            this.chatController.StartUp();
        }

        private void mainWindow_Closing(object sender, CancelEventArgs e) {
            if (this.chatController != null) {
                this.chatController.NotifyUserLeft(App.User);
                this.chatController.Close();
            }
        }

        #region chatController EventHandlers

        private void chatController_Started(object sender, EventArgs e) {
            txtMessage.IsEnabled = true;
        }

        private void chatController_UserJoined(object sender, UserEventArgs e) {
            lstMessages.Items.Add("User joined: " + e.User);
        }

        private void chatController_UserLeft(object sender, UserEventArgs e) {
            lstMessages.Items.Add("User left: " + e.User);
        }

        private void chatController_MessageReceived(object sender, MessageEventArgs e) {
            lstMessages.Items.Add(e.From + " says: " + e.Message);
        }

        private void chatController_ListUpdated(object sender, UserListEventArgs e) {
            lstUsers.Items.Clear();
            foreach (string user in e.AllUsers) {
                lstUsers.Items.Add(user);
            }
        }

        private void chatController_Whipered(object sender, MessageEventArgs e) {
            lstMessages.Items.Add(e.From + " says: " + e.Message);
        }

        #endregion //chatController EventHandlers

        private void btnSend_Click(object sender, RoutedEventArgs e) {
            this.chatController.SendMessage(App.User, this.txtMessage.Text);
            this.txtMessage.Text = string.Empty;
            this.txtMessage.Focus();
            this.btnSend.IsEnabled = false;
            this.btnWhisper.IsEnabled = false;
        }

        private void btnWhisper_Click(object sender, RoutedEventArgs e) {
            this.chatController.Whisper(App.User, this.lstUsers.SelectedItem as string, this.txtMessage.Text);
            this.txtMessage.Text = string.Empty;
            this.txtMessage.Focus();
            this.btnSend.IsEnabled = false;
            this.btnWhisper.IsEnabled = false;
        }

        private void txtMessage_KeyUp(object sender, KeyEventArgs e) {
            this.btnSend.IsEnabled = !string.IsNullOrEmpty(this.txtMessage.Text);
            this.btnWhisper.IsEnabled = this.btnSend.IsEnabled && lstUsers.SelectedItem != null;
            if (e.Key == Key.Enter) {
                btnSend_Click(btnSend, new RoutedEventArgs());
            }
        }

        private void lstUsers_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            this.btnWhisper.IsEnabled = (this.lstUsers.SelectedItem != null) && btnSend.IsEnabled;
        }
    }
}
